package demo6;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-08-31
 * Time:14:28
 */
//接口使用实例

//给对象数组排序，使用 Comparable 接口 并重写（实现）其中的 compareTo 抽象方法
class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name,int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        /*if (this.age > o.age) {
            return 1;
        }else if (this.age == o.age) {
            return 0;
        }else {
            return -1;
        }*/
        return o.age - this.age;//按年龄比较大小-从大到小
        //return this.name.compareTo(o.name);//按姓名比较
    }
}

//交换两个数的数值
class MyValue {
    public int val;
}
public class TestComparable {
    public static void swap(MyValue myV1,MyValue myV2) {
        int tmp = myV1.val;
        myV1.val = myV2.val;
        myV2.val = tmp;
    }
    public static void main(String[] args) {
        MyValue myValue1 = new MyValue();
        MyValue myValue2 = new MyValue();

        myValue1.val = 10;
        myValue2.val = 20;
        System.out.println("交换前：" + myValue1.val + " " + myValue2.val);
        swap(myValue1, myValue2);
        System.out.println("交换后：" + myValue1.val + " " + myValue2.val);
    }
    public static void main3(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",10);
        students[1] = new Student("lisi",19);
        students[2] = new Student("ahua",8);

        System.out.println("排序前" + Arrays.toString(students));

        //NameComparable nameComparable = new NameComparable();
        //Arrays.sort(students,nameComparable);

        AgeComparable ageComparable = new AgeComparable();
        Arrays.sort(students,ageComparable);
        //mySort(students);

        System.out.println("排序后" + Arrays.toString(students));
    }
    private static void mySort(Comparable[] comparables) {
        //比较的趟数
        for (int i = 0; i < comparables.length-1; i++) {
            //一个数据比较的趟数
            for (int j = 0; j < comparables.length-1-i; j++) {
                if (comparables[j].compareTo(comparables[j+1]) > 0) {
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = tmp;
                }
            }
        }
    }
    public static void main2(String[] args) {
        //多个学生对象进行比较
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",10);
        students[1] = new Student("lisi",19);
        students[2] = new Student("ahua",8);

        System.out.println("排序前" + Arrays.toString(students));
        //Arrays.sort(students);
        mySort(students);
        System.out.println("排序后" + Arrays.toString(students));
    }
    public static void main1(String[] args) {
        Student student1 = new Student("zhangsan",10);
        Student student2 = new Student("lisi",19);

        //System.out.println(student1 < student2);

        //System.out.println(student1.compareTo(student2));

        AgeComparable ageComparable = new AgeComparable();
        int ret = ageComparable.compare(student1,student2);
        System.out.println(ret);
    }
}
